import request from './request'
import { useUserStore } from '../store/modules/user'
import { UploadRawFile } from 'element-plus'
import { AxiosPromise } from 'axios'
// 产品附件上传
export function proAnnexUpload(fileList: any) {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/goods',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 工单附件上传
 * @param {Array<UploadRawFile>} fileList 文件列表
 */
export function orderAnnexUpload(
  fileList: Array<UploadRawFile>,
): AxiosPromise<APIResponseRows<Array<string>>> {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/order',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

// 用户附件上传
export function userAnnexUpload(fileList: any) {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/user',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 知识中心附件上传
 * @param {Array<UploadRawFile>} fileList - 文件列表
 */
export function knowledgeAnnexUpload(
  fileList: Array<UploadRawFile>,
): AxiosPromise<APIResponseList<string>> {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/knowledge',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

// 删除文件
export function deleteFile(data: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/file/delete/',
    method: 'delete',
    headers: token,
    data,
  })
}

// 视频上传
export function videoUpload(fileList: any) {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/video/knowledgeUpload',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}
// 条码附件上传
export function codeUpload(fileList) {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/codeUpload',
    method: 'post',
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
    data: files,
  })
}
// 头像上传
export function userAnnex(files) {
  const file = new FormData()
  file.append('file', files)
  return request({
    url: '/file/annex/avatarUpload',
    method: 'post',
    data: file,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 工单视频上传
 * @param {Array<UploadRawFile>} fileList - 文件列表
 */
export function orderVideoUpload(
  fileList: Array<UploadRawFile>,
): AxiosPromise<APIResponseRows<Array<string>>> {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/video/orderVideoUpload',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

// 系统附件上传
export function systemAcceUpload(fileList: any) {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/sysUpload',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}
// ********************导入类接口************************
// 公司档案导入
export function userExcelImport(file, uuid) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: '/import/userExcelImport/' + uuid,
    method: 'post',
    data: isFile,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}
// 工程师资料导入
export function networkEmpImport(file, uuid) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: '/import/networkEmpImport/' + uuid,
    method: 'post',
    data: isFile,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}
// 产品资料导入
export function goodsExcelImport(file, type, uuid) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/goodsExcelImport/${type}/${uuid}`,
    method: 'post',
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
    data: isFile,
  })
}

/**
 * 产品BOM导入
 * @param {(File|UploadRawFile)} file - 文件对象
 * @param {Number} uuid - 上传唯一标识
 */
export const goodBOMImport = (file: File | UploadRawFile, uuid: number) => {
  const fileList = new FormData()
  fileList.append('file', file)
  return request({
    url: `/import/goodsBoomImport/${uuid}`,
    method: 'post',
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
    data: fileList,
  })
}

// 网点资料导入
export function networkImport(file, uuid) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/networkImport/${uuid}`,
    method: 'post',
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
    data: isFile,
  })
}
// 条码档案导入
export function codeImport(file, uuid) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/codeImport/${uuid}`,
    method: 'post',
    data: isFile,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}
// *********************end**********************
// 产品导出Excel
export function goodsExportExcel(data) {
  return request({
    url: '/dataExport/goodsExportExcel',
    method: 'post',
    data,
    responseType: 'blob',
  })
}
/**
 * 工单资料导入
 * @param {string} uuid - uuid进度识别码
 * @param {object} file - 文件
 */
export function orderImport(uuid: string | number, file: UploadRawFile) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/orderImport/${uuid}`,
    method: 'post',
    data: isFile,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}
/**
 * 历史工单资料导入
 * @param {string} uuid - uuid进度识别码
 * @param {object} file - 文件
 */
export function historyOrderImport(uuid: string | number, file: UploadRawFile) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/historyOrderImport/${uuid}`,
    method: 'post',
    data: isFile,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 网点已发放金额导入
 * @param uuid - uuid进度识别码
 * @param file - 文件
 */
export function networkIssuedAmountImport(file: UploadRawFile, uuid: string | number) {
  const isFile = new FormData()
  isFile.append('file', file)
  return request({
    url: `/import/grantAmountImport/${uuid}`,
    method: 'post',
    data: isFile,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 导出提现单
 */
export function exportWithdrawToExcel(data) {
  return request({
    url: '/withdraw/exportWithdrawToExcel',
    method: 'post',
    data,
    responseType: 'blob',
  })
}

/**
 * 网点文件上传
 * @param {Array<UploadRawFile>} fileList - 文件列表
 */
export const networkFileUpload = (
  fileList: Array<UploadRawFile>,
): AxiosPromise<NetworkAPIResponse.FileUpload> => {
  const files = new FormData()
  fileList.forEach((item) => {
    files.append('files', item)
  })
  return request({
    url: '/file/annex/networkUpload',
    method: 'post',
    data: files,
    headers: { 'access-token': useUserStore().token, 'Content-Type': 'multipart/form-data' },
  })
}

/**
 * 文件上传
 * @param files - 文件列表
 */
export const fileUpload = (
  files: Array<UploadRawFile | File>,
): AxiosPromise<APIResponseRows<Array<string>>> => {
  const _files = new FormData()
  files.forEach((item) => {
    _files.append('files', item)
  })
  return request({
    url: '/file/annex/annexUpload',
    method: 'post',
    data: _files,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}
